package com.augurit.gis.location;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.augurit.gis.common.dao.CommonDAO;
import com.augurit.gis.common.util.SQLUtil;

public class FlexLocationDAO extends CommonDAO {

	private List<LocationParam> list;
	private List<String> roadKeyString;
	public FlexLocationDAO() {
		roadKeyString=new ArrayList<String>();
		roadKeyString.add("路");
		roadKeyString.add("号");
		roadKeyString.add("道");
		roadKeyString.add("街");
		roadKeyString.add("巷");
		roadKeyString.add("高速");
		roadKeyString.add("高架");
		roadKeyString.add("里");
		roadKeyString.add("弄");
		roadKeyString.add("段");
		roadKeyString.add("条");
	}
	
	private String m_keyValue="";
	
	/**
	 * 查询
	 * @param LayerName 地图服务中的图层名称，对应flex中图层选项卡中的选项名称
	 * @param tableName SDE数据库中，查询的的对象的表
	 * @param keyWord 查询的列
	 * @param keyWordSpell 查询的拼音列
	 * @param keyValue 查询的值
	 * @return
	 */
	public List<LocationItem> getList(String tableName,String keyWord,String keyWordSpell,String keyValue){
		String sql;
		m_keyValue=keyValue;
	    List<LocationItem> itemList = new ArrayList<LocationItem>();
	    if(keyValue=="") return itemList;
		//如果keyWordSpell为空，那么就不进行首字母搜索
		if(keyWordSpell==null||keyWordSpell=="")
		{
			 sql  = "select objectid,"+keyWord+" as keyname from "+tableName+" where 1=0";
			 sql += " or "+keyWord+" like '%"+SQLUtil.convertString4Sql(keyValue)+"%'";
		}
		else
		{
			 sql  = "select  objectid,"+keyWord+" as keyname from "+tableName+" where 1=0";
			 sql += " or "+keyWord+" like '%"+SQLUtil.convertString4Sql(keyValue)+"%'";
			 sql += " or "+keyWordSpell+" like '%"+SQLUtil.convertString4Sql(keyValue.toUpperCase())+"%'";
		}
		System.out.println(sql);
		try {
			//return commonSearch(buildSQL(0, 20, sql),com.augurit.gis.common.dao.ConnectionFactory.getMisConnection());
			itemList=commonSearch(buildSQL(0, 20, sql),com.augurit.gis.common.dao.ConnectionFactory.getMisConnection());
			//如果没有查询出结果，则下一步分词
			if(itemList.isEmpty())
			{
				return getList(tableName,keyWord,keyWordSpell,getNextKeyValue(keyValue));
			}
			else
			{
				return itemList;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String getNextKeyValue(String keyValue)
	{
		String tKeyValue="";
		for(int i=keyValue.length()-1;i>0;i--)
		{
			//处理单个关键字
			if(roadKeyString.indexOf(keyValue.substring(i-1,i))!=-1)
			{
				tKeyValue=keyValue.substring(0,i);
				System.out.println("-------------------------------------"+tKeyValue+"--------------------------------------");
				break;
			}
			//处理两个关键字
			else if(i>1&&roadKeyString.indexOf(keyValue.substring(i-2,i))!=-1)
			{
				tKeyValue=keyValue.substring(0,i);
				System.out.println("-------------------------------------"+tKeyValue+"--------------------------------------");
				break;
			}
		}
		return tKeyValue;
	}
		
	@SuppressWarnings("unchecked")
	@Override
	public List resultSetToList(ResultSet set) throws SQLException {
		List<LocationItem> itemList = new ArrayList<LocationItem>();
		while(set.next()){
			LocationItem item = new LocationItem();
			item.setTitle(set.getString("keyname"));
			item.setObjectId(set.getString("objectId"));
			item.setKeyValue(m_keyValue);
			itemList.add(item);
		}
//		if(itemList.isEmpty())
//		{
//			getList(m_tableName,m_keyWord,m_keyWordSpell,m_keyValue);
//		}
//		else
//		{
			return itemList;
//		}
		
	}
	private LocationParam getTableName(String layerName) {
		for(LocationParam param : list){
			if(param.getLayerName().equals(layerName)){
				return param;
			}
		}
		return null;
	}
}
